import { JXDef } from "../../../conventions/JXCommon";
import GViewBase from "../../../Core/GView/GViewBase";
import { VIEW_ID } from "../../Common/UI";
import RJXItem from "../../Data/RunTime/RunRaw/RJXItem";
import GameMgr from "../../Logic/GameMgr";
import JXItem from "../Common/JXItem";
import { GCtrl } from './../../../Core/GCtrl';
import { CMsg, ITEM_DETAIL_FLAG, PART_BTL_SCENE, PART_ITEM } from './../../Common/Define';
import { Res } from './../../Common/UIResources';
import { JXBattleAnalysis } from './../JXBattle/JXBattleAnalysis';
import JXResultHero from "./JXResultHero";

const { ccclass, property, menu } = cc._decorator;
@ccclass @menu("View/Fight/BattleRewardCtrl")
export default class BattleRewardCtrl extends GViewBase {

    @property(cc.Node) rewardLayer: cc.Node = null;
    @property(cc.Node) heroLayer: cc.Node = null;
    @property(cc.Node) double: cc.Node = null;


    private _ans: JXBattleAnalysis = null;
    private _cb: Function = null;
    private _isWin: boolean = null;
    private _levelRaw: SStoryDataRaw | STowerDataRaw = null;
    private _TowerData: STowerDataRaw = null
    private _secenId: number = null;
    private _star: number = 0;
    private _reward: number[][] = null;
    public onGStart(secenId: number, level: number, ans: JXBattleAnalysis, cb: Function): void {
        this._secenId = secenId;
        this._ans = ans;
        // console.log('this._ans', this._ans);

        this._cb = cb;
        this._isWin = this._ans.isDefWin;
        if (this._secenId == PART_BTL_SCENE.ARENA) {
            // this._levelRaw = GameMgr.towerData.getRaw<STowerDataRaw>(level);
            this._TowerData = GameMgr.towerData.getRaw<STowerDataRaw>(level);
        } else {
            this._levelRaw = GameMgr.storyData.getRaw<SStoryDataRaw>(level);
        }
        this.win.maskNode.opacity = 200;
        this.setView()
    }

    protected setView() {
        let star = 0;
        let reward: number[][] = [];
        switch (this._secenId) {
            case PART_BTL_SCENE.ARENA: {
                let levelRaw = this._levelRaw as STowerDataRaw;
                if (this._isWin) {
                    GameMgr.lUserData.sportsLv = this._TowerData.id;
                    reward = this._TowerData.winAward;
                } else {
                    reward = this._TowerData.lose;
                }
                break;
            }
            case PART_BTL_SCENE.GUANQIA: {
                let levelRaw = this._levelRaw as SStoryDataRaw;
                if (this._isWin) {
                    let starTarget = levelRaw.win;
                    starTarget.forEach(element => {
                        if (this._ans.barrackHps >= element) {
                            star++;
                        }
                    });
                    reward = this._levelRaw["star" + star];
                    GameMgr.lUserData.level = levelRaw.id;
                } else {
                    reward = this._levelRaw.lose;
                }
                break;
            }
        }
        this._reward = reward;
        this._star = star;
        this.setReward(reward);
        this.setHeroHead();
    }
    protected setReward(reward: number[][]) {
        this.assetImpl.prefab(Res.prefab.item.jxItem, (prefab: cc.Prefab) => {
            for (let i = 0; i < reward.length; i++) {
                let rjxItem = new RJXItem(reward[i]);
                let node = cc.instantiate(prefab);
                let comp = node.getComponent(JXItem);
                node.setScale(1.2);
                comp.setView(rjxItem, ITEM_DETAIL_FLAG.LIGHT | ITEM_DETAIL_FLAG.SHOWLIGHT);
                this.rewardLayer.addChild(node)
            }
        })
    }



    protected setHeroHead() {
        let basic = GameMgr.systemConfig.value<number>(JXDef.SYS_CONFIG_KEY.basic);

        var allExp = this._star > 0 ? this._levelRaw.exp[this._star - 1] : 0;
        if (this._secenId == PART_BTL_SCENE.ARENA) {
            if (this._isWin) {
                allExp = this._TowerData.exp[1];
            } else {
                allExp = this._TowerData.exp[0];
            }


        }
        let basicExp = Math.round(allExp * basic);
        let otherExp = allExp - basicExp;
        this.assetImpl.prefab(Res.prefab.vw.fight.resultHero, (prefab: cc.Prefab) => {
            this._ans.defTPL.partnerData.forEach((v: IHeroBtlData, k: number) => {
                let node = cc.instantiate(prefab);
                let comp = node.getComponent(JXResultHero);
                node.scale = 0.9;
                node.x = -145;
                let hurt = this._ans.heroHurdInfo.get(v.tableId);
                hurt = hurt ? hurt : 0;
                let exp = Math.round(basicExp / this._ans.heroHurdInfo.size + otherExp * (hurt / this._ans.allHurt));
                // console.log("exp", exp);
                if (!exp) {
                    exp = 0;
                }
                comp.setView(v.tableId, exp, this._cb);
                if (this._secenId == PART_BTL_SCENE.GUANQIA) {
                    comp.openUp();
                }
                this.heroLayer.addChild(node);
            })
        })
    }

    protected onGetRewardClick(_, isDouble: string) {
        let doubleGet = Number(isDouble);
        if (doubleGet) {
            GameMgr.sdkMgr.watchAd(() => {
                this.double.active = false;
                this._reward.forEach((ele, index) => {
                    this._reward[index][PART_ITEM.NUM] += ele[PART_ITEM.NUM]
                })
                GCtrl.ES.emit(CMsg.client.bag.onBagItemChange, this._reward)
                this.onClose();
            }, () => {
            })
        } else {
            GCtrl.ES.emit(CMsg.client.bag.onBagItemChange, this._reward);
            this.onClose();
        }
    }

    public onClose(): void {
        super.onClose();
        if (this._cb) {
            this._cb();
        }

        if (this._secenId != PART_BTL_SCENE.ARENA) {
            GameMgr.jumpToMgr.jumpGoTo(VIEW_ID.mainPage);
        }
        GCtrl.ES.emit(CMsg.client.home.onaddLevel);
    }
}